package com.atguigu.juc.volatiledemo;

import java.util.concurrent.TimeUnit;

/**
 * @Description: volatile 不能保证原子性
 * @author： yeswater
 * @create： 2024/5/30
 */


public class VolatileNoAtomicDemo {
	
	private static class MyNumber {
		static volatile int num;
		
		static public void add(){
			//public synchronized void add(){
			num++;
		}
	}
	
	public static void main(String[] args) {
		for (int i = 0; i < 10; i++) {
			new Thread(() -> {
				System.out.println(Thread.currentThread().getName() + " start num=" + MyNumber.num);
				for (int i1 = 0; i1 < 1000; i1++) {
					MyNumber.add();
				}
			}, String.valueOf(i)).start();
		}
		
		try {
			TimeUnit.SECONDS.sleep(2);
		} catch (InterruptedException e) {
			throw new RuntimeException(e);
		}
		System.out.println(MyNumber.num);
	}
	
}
